package com.leetcode.algorithm.y22.m07.w4;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.leetcode.algorithm.common.TreeNode;

class BSTIterator {

	private List<Integer> list;
	private Iterator<Integer> iterator;

	public BSTIterator(TreeNode root) {
		list = new ArrayList<>();
		inorderTraversal(root, list);
		iterator = list.iterator();
	}

	private void inorderTraversal(TreeNode root, List<Integer> list) {
		if (root == null) {
			return;
		}
		inorderTraversal(root.left, list);
		list.add(root.val);
		inorderTraversal(root.right, list);
	}

	public int next() {
		return iterator.next();
	}

	public boolean hasNext() {
		return iterator.hasNext();
	}
}